//
//  VideoListViewModel.m
//  VideoPlayer
//
//  Created by 白璐 on 16/9/18.
//  Copyright © 2016年 baidu. All rights reserved.
//

#import "VideoListViewModel.h"
#import "VideoListModel.h"
#import "DownloadViewModel.h"

@interface VideoListViewModel ()
@property(nonatomic, strong) VideoListModel* model;
@end

@implementation VideoListViewModel

- (instancetype)init {
    if (self = [super init]) {
        _model = [[VideoListModel alloc] init];
        [_model loadResource];
    }
    return self;
}

- (void)dealloc {
}

- (NSUInteger)count {
    return self.model.videoList.count;
}

- (NSString*)image:(NSUInteger)index {
    return self.model.videoList[index].img;
}

- (NSString*)title:(NSUInteger)index {
    return self.model.videoList[index].title;
}

- (NSString*)detail:(NSUInteger)index {
    return self.model.videoList[index].detail;
}

- (NSString*)url:(NSUInteger)index {
    return self.model.videoList[index].url;
}

- (NSString*)token:(NSUInteger)index {
    return self.model.videoList[index].token;
}

- (BOOL)isDownloadableResource:(NSUInteger)index {
    NSString* url = [self url:index];
    url = url.lowercaseString;
    return [url hasPrefix:@"http"] && [url hasSuffix:@"m3u8"];
}

- (BOOL)isDownloading:(NSUInteger)index {
    NSString* url = [self url:index];
    return [[DownloadViewModel defaultModel] isDownloading:url];
}

- (void)deleteVideo:(NSUInteger)index {
    [self.model.videoList removeObjectAtIndex:index];
}

- (void)add:(NSString*)name url:(NSString*)url {
    VideoItem* item = [[VideoItem alloc] init];
    item.img = @"no_resource";
    item.title = name;
    item.detail = name;
    item.url = url;
    [self.model.videoList addObject:item];
}

- (void)download:(NSUInteger)index cancelBlock:(void(^)(BOOL cancel))block {
    VideoItem* item = self.model.videoList[index];
    [[DownloadViewModel defaultModel] add:item cancelBlock:block];
}

#pragma mark - VideoSource
- (void)downloadVideo:(NSString*)url cancelBlock:(void(^)(BOOL cancel))block{
    for (VideoItem* item in self.model.videoList) {
        if ([item.url isEqualToString:url]) {
            [[DownloadViewModel defaultModel] add:item cancelBlock:block];
            break;
        }
    }
}

- (VideoItem*)previousVideo:(NSString*)url {
    NSInteger index = 0;
    for (VideoItem* item in self.model.videoList) {
        if ([item.url isEqualToString:url]) {
            if (--index >= 0) {
                return self.model.videoList[index];
            }
            return nil;
        }
        
        ++index;
    }
    return nil;
}

- (VideoItem*)nextVideo:(NSString*)url {
    NSInteger index = 0;
    for (VideoItem* item in self.model.videoList) {
        if ([item.url isEqualToString:url]) {
            if (++index < self.model.videoList.count) {
                return self.model.videoList[index];
            }
            return nil;
        }
        
        ++index;
    }
    return nil;
}

- (BOOL)downloadable:(NSString*)url {
    return [[DownloadViewModel defaultModel] isDownloading:url];
}

@end
